class Solution:

    def minimumOperations(self, num: str) -> int:
        # 0,25,50,75
        n = len(num)
        num = num[::-1]

        def f(a):
            return num.index(a) if a in num else n
        
        z, five = f("0"), f("5")
        ans = n - 1 if z < n else n
        if z < n:
            ans = ans if "0" not in num[z + 1::] else num[z + 1::].index("0") + z
            k = n if "5" not in num[z + 1::] else num[z + 1::].index("5") + z
            ans = min(ans, k)
        if five < n:
            k = n if "2" not in num[five +
                                       1::] else num[five +
                                                     1::].index("2") + five
            ans = min(ans, k)
        if five < n:
            k = n if "7" not in num[five +
                                    1::] else num[five + 1::].index("7") + five
            ans = min(k, ans)
        return ans

class Solution:
    def minimumOperations(self, num: str) -> int:
        n = len(num)
        def f(tail: str) -> int:
            i = num.rfind(tail[1])
            if i < 0: return n
            i = num.rfind(tail[0], 0, i)  # 写成 num[:i].rfind(tail[0]) 会产生额外的切片空间
            if i < 0: return n
            return n - i - 2
        return min(n - ('0' in num), f("00"), f("25"), f("50"), f("75"))

s = Solution()
print(s.minimumOperations("5316527164518463841638493134993466318994774"))

num = "12345"
num.rfind()
# print(num.index("2"), num[::-1])
# # print(num.index("6"))